<?php
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: nonant <nonant@thinksns.com>
// +----------------------------------------------------------------------
//

class ColleagueAction extends Action{
	
	function _initialize(){
		$this->setTitle( L('index') );
	}		
	
	//同事首页
	function index(){
		$map = 'a.uid ='.$this->mid;
		if($_GET['g']) $map.=' AND b.gid='.intval($_GET['g']);

		$pColleague = D('Colleague');
		
		$page = $pColleague->query("SELECT count(DISTINCT a.fid) as c FROM ".C('DB_PREFIX')."user_colleague a LEFT JOIN ".C('DB_PREFIX')."user_colleague_relation b ON a.cid=b.cid WHERE $map");
		$list = $pColleague->table(C('DB_PREFIX')."user_colleague a LEFT JOIN ".C('DB_PREFIX')."user_colleague_relation b ON a.cid=b.cid")->where($map)->field('DISTINCT(a.fid),a.*')->findpage(8,$page[0]['c']);
		$grouplist = $pColleague->getUserGroup($this->mid);
		foreach ($list['data'] as $key=>$val){
			$arrgids =  getSubByKey( D('Colleague')->getGroup( $val['cid'] ) , 'gid' );
			foreach($arrgids as $k=>$v){
				$list['data'][$key]['gname'][$k] = $grouplist[$v];
			}
		}
		$this->assign($list);
		$this->assign('grouplist',$grouplist);
		$this->display();
	}

	//访客
	function track(){
		$type = $_GET['type']?$_GET['type']:'m';
		if($type=='m'){
			$map['uid'] = $this->mid;
		}else{
			$map['visitor'] = $this->mid;
		}
		$list = M('space_visitor')->where($map)->order('cTime DESC')->findpage(20);
		$this->assign('type',$type);
		$this->assign($list);
		$this->display();
	}

	//搜索同事
	function search(){
		if($_POST){
			if(empty($_POST['uname']) && empty($_POST['username'])){
				$this->error(L('no_empty_search'));
			}else{
				if($_POST['uname']){
					$map['uname'] =  array('like','%'.$_POST['uname'].'%');
				}
				if($_POST['username']){
					$map['fullname'] = array('like','%'.$_POST['username'].'%');
				}
				$list = M('User')->where($map)->field('fullname,uid')->findall();
				foreach ($list as $key=>$value){
					$list[$key]['record'] = D("Record")->getNewData($value['uid']);
				}
				$this->assign('list',$list);
				$this->assign('grouplist',D('Colleague')->getUserGroup($this->mid));

			}
		}
		$this->display();
	}

	//添加联系人
	function add(){

		if($_POST){
			$this->transact();
		}else{
			$this->assign('grouplist',D('Colleague')->getUserGroup($this->mid));
			$this->display();
		}
	}
	
	//添加过滤
	private function transact(){
		$pColleague = D('Colleague');
		$arr = $pColleague->__checkParseData($_POST['uname']);
		$this->assign('gid',intval($_POST['gid']));
		$this->assign('findlist',$arr);
		$this->assign('grouplist',$pColleague->getUserGroup($this->mid));
		$this->display('transact');
	}

	function parsedata(){
		$strContent = $_POST['content'];
		$list = D('Colleague')->__parseData($strContent);
		exit(json_encode($list));
	}
	
	//执行添加操作
	function doadd(){
		$pColleague = D('Colleague');
		if($_POST['type']=='havaInit'){
			$result = $pColleague->doaddAll($this->mid,$_POST['haveInit']);
			if($result){
				foreach ($result as $v){
					$html[]="<a href='".U('space/View/index',array('uid'=>$v))."' target='_blank'>".getUserName($v)."</a>";
				}
				$info['list']   = implode(',',$html);
				$info['number'] =  count($result);
				$this->success($info);
			}else{
				$this->error(L('fail_import'));
			}
		}elseif ($_POST['type']=='unInit'){
			foreach ($_POST['unInit']['uname'] as $key=>$value){
				$data['uname']       = $value;
				$data['gid']         = $_POST['unInit']['gid'][$key];
				$data['attention']   = $_POST['unInit']['attention'][$key];
				$data['remark']      = $_POST['unInit']['remark'][$key];
				$pColleague->doinvite($this->mid,$_POST['invitemessage'],$data);  //发送邀请
				$uname[] = $value;
			}
			if($uname){
				$info['list']   = implode(',',$uname);
				$info['number'] =  count($uname);				
				$this->success($info);
			}else{
				$this->error(L('fail_operation'));
			}
		}else{
			echo 'wrong';
		}
	}
	
	function doaddone(){
		$intGid = intval($_POST['gid']);
		$strRemark = h($_POST['remark']);
		if($intGid){
			echo D('Colleague')->doaddOne($this->mid,$_POST['fid'],$intGid,$strRemark);
		}else{
			echo '0'; //选择同事分组
		}
	}

	//编辑同事
	function editFriend(){
		$pColleague = D('Colleague');
		$intCid = intval($_REQUEST['cid']);
		$info = $pColleague ->where("cid={$intCid} AND uid={$this->mid}")->find();
		$checked   =  $pColleague ->getGroup( $intCid );
		$checked   =  getSubByKey( $checked , 'gid' );
		$this->assign('info',$info);
		$this->assign('arrchecked',$checked);
		$this->assign('grouplist',$pColleague->getUserGroup($this->mid));
		$this->assign('cid',$intCid);
		$this->display();
	}
	
	
	//同事管理
	function managecolleague(){
		$pColleague = D('Colleague');
		if($_POST['type']=='del'){
			if($pColleague->where("uid=".$this->mid." AND cid=".intval($_POST['cid']))->delete()){
				$pColleague->execute("DELETE FROM ".C('DB_PREFIX')."user_colleague_relation WHERE cid=".intval($_POST['cid']) );
				$this->success();
			}else{
				$this->error(L('fail_delete'));
			}
		}elseif ($_POST['type']=='edit'){
			$intCid      = intval( $_POST['cid'] );
			$info = $pColleague->where("cid={$intCid} AND uid={$this->mid}")->find();
			$gids        = $_POST['gids'];

			$pColleague->setField('remark',$_POST['remark'],"cid=$intCid" );
			
			$pColleague->doeditGroup( $intCid , $gids);
			$grouplist = $pColleague->getUserGroup($this->mid);
			foreach( $gids as $key=>$value ){
				$arr[] = $grouplist[$value];
			}
			$this->success( implode(',',$arr) );
 		}
	}

	//用户组管理
	function managegroup(){
		$pColleagueGroup = M('user_colleague_group');
		if($_POST['type']=='add'){
			$strGroupName = h($_POST['groupname']);
			if($strGroupName){
				$pColleagueGroup = D('user_colleague_group');
				$is_Exist = $pColleagueGroup->where("uid={$this->mid} AND name='{$strGroupName}'")->count();
				if( $is_Exist ){
					$this->error(L('group_unavailable', array('$i'=>$strGroupName)));
				}else{
					$data['uid']  = $this->mid;
					$data['name'] = $strGroupName;
					if( $gid = $pColleagueGroup->add( $data ) ){
						$this->success($gid);
					}else{
						$this->error(L('fail_add'));
					}
				}
			}else{
				$this->error(L('no_empty_group'));
			}
		}elseif($_POST['type']=='del'){
			$intGid = intval( $_POST['id'] );
			$countColleague = M('user_colleague_relation')->where('gid='.$intGid)->count();
			if( $countColleague ){
				$this->error($countColleague);
			}else{
				$group_list =  $pColleagueGroup->where('uid='.$this->mid.' AND id='.intval($_POST['id']))->delete();
				$this->success();
			}
		}elseif($_POST['type']=='movetodel'){		
			$intId   = intval( $_POST['id'] );
			$intToid = intval( $_POST['toid'] );
			if( $intToid!=0 ){
				M('user_colleague_relation')->setField('gid',$intToid,'gid='.$intId);
			}
			M('user_colleague_relation')->where('gid='.$intId)->delete();
			
			$pColleagueGroup->where('uid='.$this->mid.' AND id='.intval($_POST['id']))->delete();
			$this->success();
		}elseif($_POST['type']=='edit'){
			$strGroupName = trim($_POST['groupname']);
			if(!$strGroupName) $this->error(L('no_empty_group'));
			$intGroupId   = intval($_POST['id']);
			$is_Exist = $pColleagueGroup->where("uid={$this->mid} AND name='{$strGroupName}' AND id <> $intGroupId")->count();
			if( $is_Exist ){
				$this->error(L('group_unavailable', array('$i'=>$strGroupName)));
			}else{
				if( $pColleagueGroup->setField('name',$strGroupName,'uid='.$this->mid.' AND id='.$intGroupId) ){
					$this->success(L('success_save'));
				}
			}	
		}
	}

	function changenew(){
		$intGid = intval( $_GET['gid'] );
		$group_list = M('user_colleague_group')->where('uid='.$this->mid.' AND id<>'.$intGid)->field('id,name')->order('id ASC')->findall();
		foreach( $group_list as $value){
			$grouplist[ $value['id'] ] = $value['name'];
		}
		$this->assign('grouplist',$grouplist);
		$this->assign('gid',$intGid);
		$this->display();
	}
	
	//我关注的人
	function followYou(){
		$dao = service('Follow');

		$map['uid'] = $this->mid;
		$map['type'] = 'user';
		$list = $dao->getFollow($map,'aimId',55);
		foreach($list['data'] as &$v){
			$v = $v['aimId'];
		}
		$this->assign($list);

		$this->display();
	}

	//关注我的人
	function followMe(){
		$dao = service('Follow');

		$map['aimId'] = $this->mid;
		$map['type'] = 'user';
		$list = $dao->getFollow($map,'uid',55);
		foreach($list['data'] as &$v){
			$v = $v['uid'];
		}
		$this->assign($list);

		$this->display();
	}
}
?>